package com.qianji.petmall.order.listener;

import com.qianji.petmall.order.entity.OrderEntity;
import com.qianji.petmall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author liang zai
 * @date 2022/5/13 16:45
 * @description
 */
//关闭订单
@Component
@RabbitListener(queues = "order.release.queue")
public class OrderCloseListener {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity order, Channel channel, Message message) throws IOException {
        System.out.println("收到过期的订单信息，准备关闭订单："+order.getOrderSn());
        try {
            orderService.closeOrder(order);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            //关闭失败重新丢经队列中
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }


}
